package com.naodong.shenluntiku.mvp.view.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.text.Html;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewStub;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.binaryfork.spanny.Spanny;
import com.google.android.flexbox.FlexboxLayout;
import com.naodong.shenluntiku.R;
import com.naodong.shenluntiku.mvp.model.bean.EventBusTag;
import com.naodong.shenluntiku.mvp.model.bean.SubjectInfo;
import com.naodong.shenluntiku.mvp.model.bean.SubjectOption;
import com.yatatsu.autobundle.AutoBundleField;

import butterknife.BindView;
import me.shingohu.man.base.BaseMVPFragment;
import me.shingohu.man.di.component.AppComponent;
import me.shingohu.man.intergation.EventManager;
import me.shingohu.man.util.UiUtils;

/**
 * Created by hujie on 17/5/10.
 */

public class SubjectInfoFragment extends BaseMVPFragment {


    @AutoBundleField(required = false)
    SubjectInfo subjectInfo;

    @AutoBundleField(required = false)
    boolean showAnalysis;//显示解析

    @BindView(R.id.materialTV)
    TextView materialTV;

    @BindView(R.id.materialView)
    View materialView;

    @BindView(R.id.subjectTV)
    TextView subjectTV;

    @BindView(R.id.subjectTitle)
    TextView subjectTitle;

    @BindView(R.id.optionView)
    LinearLayout optionContainerView;
    LayoutInflater layoutInflater;

    @BindView(R.id.subjectResultView)
    ViewStub subjectResultView;


    public static SubjectInfoFragment getInstance(boolean showAnalysis, SubjectInfo subjectInfo) {
        SubjectInfoFragment frgment = new SubjectInfoFragment();
        frgment.setSubjectInfo(subjectInfo);
        frgment.setShowAnalysis(showAnalysis);
        return frgment;
    }

    @Override
    protected boolean userAutoBindBundle() {
        return true;
    }


    public void setShowAnalysis(boolean showAnalysis) {
        this.showAnalysis = showAnalysis;
    }

    public void setSubjectInfo(SubjectInfo subjectInfo) {
        this.subjectInfo = subjectInfo;
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {

    }

    @Override
    protected void onViewCreated(Bundle savedInstanceState) {
        layoutInflater = LayoutInflater.from(getContext());
        setViewData();
    }

    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {

    }


    public void setViewData() {

        if (showAnalysis) {
            View view = subjectResultView.inflate();
            TextView zqlTV = (TextView) view.findViewById(R.id.zqlTV);//正确率
            TextView ycxTV = (TextView) view.findViewById(R.id.ycxTV);//易错项
            TextView jxTV = (TextView) view.findViewById(R.id.jxTV);//解析

            View ycxView = view.findViewById(R.id.ycxView);//易错项View

            TextView yourAnswerTV = (TextView) view.findViewById(R.id.yourAnswerTV);

            FlexboxLayout kdFlexbox = (FlexboxLayout) view.findViewById(R.id.kdFlexbox);
            zqlTV.setText(subjectInfo.getSubAvgCorrectRate() + "%");
            ycxTV.setText(subjectInfo.getSubFallibility());

            jxTV.setText(Html.fromHtml(subjectInfo.getSubExplain()));

            if (subjectInfo.getSubAbilities() != null) {
                for (String subjectAbility : subjectInfo.getSubAbilities()) {
                    TextView tagView = (TextView) layoutInflater.inflate(R.layout.ability_tag_layout, null);
                    FlexboxLayout.LayoutParams lp = new FlexboxLayout.LayoutParams(FlexboxLayout.LayoutParams.WRAP_CONTENT, FlexboxLayout.LayoutParams.WRAP_CONTENT);
                    lp.setMargins(0, 0, UiUtils.dip2px(10), UiUtils.dip2px(10));
                    tagView.setLayoutParams(lp);
                    tagView.setText(subjectAbility);
                    kdFlexbox.addView(tagView);
                }
            }


            if (subjectInfo.getSubChoiceType() == 10) {
                ycxView.setVisibility(View.GONE);
            }
            String rightAnswerString = "";
            String yourAnswerString = "";
            for (SubjectOption option : subjectInfo.getSubOptions()) {
                if (option.getOpIsCorrect() == 1) {
                    rightAnswerString += option.getOpName();
                }
                if (subjectInfo.getuChooseIds() != null) {
                    if (subjectInfo.getuChooseIds().contains(option.getOpId())) {
                        yourAnswerString += option.getOpName();
                    }
                }
            }
            if (yourAnswerString.length() == 0) {
                yourAnswerString = "未选择";
            }


            Spanny spanny = new Spanny();
            spanny.append("正确答案:  ");
            spanny.append(rightAnswerString, new ForegroundColorSpan(ContextCompat.getColor(getContext(), R.color.color72CEB7)));
            spanny.append("     你的答案:  ");
            spanny.append(yourAnswerString + "      " + (subjectInfo.getuIsCorrect() == 1 ? "回答正确" : "回答错误"), new ForegroundColorSpan(ContextCompat.getColor(getContext(), (subjectInfo.getuIsCorrect() == 1 ? R.color.color72CEB7 : R.color.colorAccent))));

            yourAnswerTV.setText(spanny);

        }
        if (TextUtils.isEmpty(subjectInfo.getSubDatum())) {
            materialView.setVisibility(View.GONE);
        } else {
            materialTV.setText(Html.fromHtml(subjectInfo.getSubDatum()));
        }
        if (subjectInfo.getSubChoiceType() == 1) {
            subjectTitle.setText("题目(单选题)");
        } else {
            subjectTitle.setText("题目(多选题)");
        }
        subjectTV.setText(Html.fromHtml(subjectInfo.getSubCon()));
        setOptionData();

    }

    private void setOptionData() {

        optionContainerView.removeAllViews();


        for (SubjectOption option : subjectInfo.getSubOptions()) {
            View optionView = layoutInflater.inflate(R.layout.item_subject_option, null);
            TextView optionIndexTV = (TextView) optionView.findViewById(R.id.optionIndexTV);
            TextView optionTV = (TextView) optionView.findViewById(R.id.optionTV);
            optionIndexTV.setText(option.getOpName());
            optionTV.setText(option.getOpCon());

            if (showAnalysis) {
                if (option.getOpIsCorrect() == 1) {
                    optionIndexTV.setBackgroundResource(R.drawable.ico_circle_right);
                    optionIndexTV.setText("");

                } else {
                    if (subjectInfo.getuChooseIds().contains(option.getOpId())) {
                        optionIndexTV.setBackgroundResource(R.drawable.ico_circle_error);
                        optionIndexTV.setText("");
                    }
                }
            } else {
                if (subjectInfo.getuChooseIds().contains(option.getOpId())) {
                    optionIndexTV.setSelected(true);
                } else {
                    optionIndexTV.setSelected(false);
                }
                optionView.setOnClickListener(v -> {
                    if (subjectInfo.getuChooseIds().contains(Integer.valueOf(option.getOpId()))) {
                        subjectInfo.getuChooseIds().remove(Integer.valueOf(option.getOpId()));
                        optionIndexTV.setSelected(false);
                    } else {
                        if (subjectInfo.getSubChoiceType() == 1) {
                            subjectInfo.getuChooseIds().clear();
                            subjectInfo.getuChooseIds().add(Integer.valueOf(option.getOpId()));
                            setOptionData();
                            //单选
                            EventManager.getInstance().postEvent(EventBusTag.TO_NEXT_SUBJECT);
                        } else {
                            subjectInfo.getuChooseIds().add(Integer.valueOf(option.getOpId()));

                        }
                        optionIndexTV.setSelected(true);
                    }
                });
            }


            optionContainerView.addView(optionView);
        }
    }


    @Override
    protected int layoutId() {
        return R.layout.f_subject_info;
    }
}
